import { app } from "/scripts/app.js";

// @ts-ignore
window._comfyui_hook_args = window._comfyui_hook_args || {};

function sendHookArgsToBackend(hook, args) {
    // try {
    //     // 只保留可序列化内容
    //     const safeArgs = Array.from(args).map(arg => {
    //         try {
    //             return JSON.parse(JSON.stringify(arg));
    //         } catch (e) {
    //             return String(arg);
    //         }
    //     });
    //     window._comfyui_hook_args[hook] = safeArgs;
    //     // 通过 HTTP 路由发送到后端
    //     fetch("/output/hooks/save", {
    //         method: "POST",
    //         headers: { "Content-Type": "application/json" },
    //         body: JSON.stringify({ hook, args: safeArgs, time: Date.now() })
    //     });
    // } catch (e) {
    //     console.warn("序列化钩子参数失败", hook, args, e);
    // }
}

app.registerExtension({
    name: "test.strictly.doc.hooks",

    async init(...args) {
        sendHookArgsToBackend("init", args);
    },

    async addCustomNodeDefs(...args) {
        sendHookArgsToBackend("addCustomNodeDefs", args);
    },

    async getCustomWidgets(...args) {
        sendHookArgsToBackend("getCustomWidgets", args);
    },

    async beforeRegisterNodeDef(...args) {
        sendHookArgsToBackend("beforeRegisterNodeDef", args);
    },

    async registerCustomNodes(...args) {
        sendHookArgsToBackend("registerCustomNodes", args);
    },

    async beforeConfigureGraph(...args) {
        sendHookArgsToBackend("beforeConfigureGraph", args);
    },

    async nodeCreated(...args) {
        sendHookArgsToBackend("nodeCreated", args);
    },

    loadedGraphNode(...args) {
        sendHookArgsToBackend("loadedGraphNode", args);
    },

    async afterConfigureGraph(...args) {
        sendHookArgsToBackend("afterConfigureGraph", args);
    },

    async setup(...args) {
        sendHookArgsToBackend("setup", args);
    }
}); 